<?php
namespace app\user\controller;
use \think\Controller;
use \think\Request;
use \think\Session;
use app\common\traits\controller\TraitController;

/**
 * 前台房车分享
 */
class Order extends Controller
{  
    use TraitController;
    // const ORDER_OBLIGATION_STATUS = 1;//未支付
    // const ORDER_STAYIN_STATUS = 2;//待入住
    // const ORDER_CHECKIN_STATUS = 3;//入住中
    // const ORDER_DONE_STATUS = 4;//已完成
    // const ORDER_CANCEL_STATUS = -99;//取消订单

    const ORDER_OBLIGATION_STATUS = 100;//未支付
    const ORDER_STAYIN_STATUS = 101;//待入住
    const ORDER_REFUND_STATUS = 102;//退款中(待入住取消订单)
    const ORDER_CANCEL_STATUS = 103;//取消订单
    const ORDER_ONGOING_STATUS = 104;//支付中
    const ORDER_CHECKIN_STATUS = 200;//入住中
    const ORDER_LEAVE_STATUS = 201;//已离开
    // const ORDER_REFUND_DEPOSIT_STATUS = 202;//返还押金中
    const ORDER_DONE_STATUS = 203;//已完成

    //前置操作
    protected $beforeActionList = [
        'checkLogin',
        'checkCaravanStatus'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    /**
     * [checkCaravanStatus 房车是否审核通过]
     * @return [type] [description]
     */
    protected function checkCaravanStatus()
    {
        //1=审核通过
        // $status = model('CaravanInfos')->where('user_id',session('user.id'))->value('status');
        // if ($status<>1) {
        //     $this->error('房车未审核通过','/caravaninfo/index');
            
        // }
    }

    
    public function getOrderList()
    {
        $list = model('Orders')->getOrders();//halt($list);
        $list1 = model('Orders')->where('rent_user_id',$this->getLoginUserInfo('id'))->select();
        $this->assign('list',$list);
        $count_list = count($list1);//halt($count_list);
        $this->assign('count_list',$count_list);

        $obligationorder = model('Orders')->getOrders(['status' =>self::ORDER_OBLIGATION_STATUS]);
        $this->assign('obligationorder',$obligationorder);//未支付
        $count_obligationorder = count($obligationorder);//halt($obligationorder);
        $this->assign('count_obligationorder',$count_obligationorder);

        $ongoingorder = model('Orders')->getOrders(['status' =>self::ORDER_ONGOING_STATUS]);
        $this->assign('ongoingorder',$ongoingorder);//支付中
        $count_ongoingorder = count($ongoingorder);
        $this->assign('count_ongoingorder',$count_ongoingorder);

        $stayin = model('Orders')->getOrders(['status' =>self::ORDER_STAYIN_STATUS]);
        $this->assign('stayin',$stayin);//待入住
        $count_stayin = count($stayin);
        $this->assign('count_stayin',$count_stayin);

        $checkin = model('Orders')->getOrders(['status' =>self::ORDER_CHECKIN_STATUS]);
        $this->assign('checkin',$checkin);//入住中
        $count_checkin = count($checkin);
        $this->assign('count_checkin',$count_checkin);

        $done = model('Orders')->getOrders(['status' =>self::ORDER_DONE_STATUS]);
        $this->assign('done',$done);//已完成
        $count_done = count($done);
        $this->assign('count_done',$count_done);

        $cancel = model('Orders')->getOrders(['status' =>self::ORDER_CANCEL_STATUS]);
        $this->assign('cancel',$cancel);//取消订单
        $count_cancel = count($cancel);
        $this->assign('count_cancel',$count_cancel);

        $refund = model('Orders')->getOrders(['status' =>self::ORDER_REFUND_STATUS]);
        $this->assign('refund',$refund);//退款中(待入住取消订单)
        $count_refund = count($refund);
        $this->assign('count_refund',$count_refund);

        $leave = model('Orders')->getOrders(['status' =>self::ORDER_LEAVE_STATUS]);
        $this->assign('leave',$leave);//已离开
        $count_leave = count($leave);
        $this->assign('count_leave',$count_leave);

        // $refund_deposit = model('Orders')->getOrders(['status' =>self::ORDER_REFUND_DEPOSIT_STATUS]);
        // $this->assign('refund_deposit',$refund_deposit);//返还押金中
        // $count_refund_deposit = count($refund_deposit);
        // $this->assign('count_refund_deposit',$count_refund_deposit);

        return view('order/orderlist');
    }



    public function getOrderDetail($id)
    {

        $orderinfo = model('Orders')->getOrderInfo(['id' => $id]);//halt($orderinfo);
        $this->assign('orderinfo',$orderinfo);
        $user_id = session('user.id');
        if ($orderinfo->rent_contact_id) {
            //$rent_user_id = $orderinfo->rent_user_id;
            $contactinfo = model('ContactInfos')->where('user_id',$user_id)->select();
            $this->assign('contactinfo',$contactinfo);
        }else{
            $contactinfo = model('ContactInfos')->order('created_at desc')->where('user_id',$user_id)->find();
            $this->assign('contactinfo',$contactinfo);
        }
        
        return view('order/orderdetail');
    }

    public function getCancelOrder($id)
    {
        //$number = model('Orders')->where('id',$id)->value('number');
        $count  = model('Orders')->cancelOrder($id);

        if ($count) {
            return $this->success('取消订单成功','/order/orderlist');
        }
        return $this->error('取消订单失败');
    }
    /**
     * 支付订单
     * @return [type] [description]
     */
    public function getPayOrder()
    {
        $user_id = $this->getLoginUserInfo('id');
        $userinfo = model('Users')->where('id',$user_id)->find();
        $intergrate = $userinfo->integral;
        //$intergrate = model('Users')->where('id',$user_id)->value('integral');

        $number = $this->request->get('order_id');

        $total_intergral = model('Orders')->where('number',$number)->value('total_intergral');
        if ($intergrate < $total_intergral) {
            return $this->error('旅行币不足');
        }

        $order = model('Orders')->getOrderInfo(['number' => $number]);
        if ($order->getData('status') == self::ORDER_OBLIGATION_STATUS) {
            $userinfo->integral = $intergrate - $total_intergral;//halt($userinfo);
            $userinfo->save();
            model('Orders')->reduceIntergrate($total_intergral,$user_id);
            $order->status = self::ORDER_ONGOING_STATUS;
            $order->save();
        }//状态为未支付--->支付中

        if ($number && $order) {
            $param  = [
                'order_id' => urlencode($order->number),
                'pt'       => urlencode('wx_pay'),
                'type'     => urlencode('order'),
                'body'     => urlencode('订单支付'),
                'detail'   => urlencode($order->toJson()), 
                'total'    => urlencode($order->total_amount)
            ];

            $sign  = $this->mixSign($param);
            $param = array_merge($param, ['sign' => $sign]);
            queue('common/JobPay@order', $order->number, 0, 'order');
            
            $this->redirect(url('/wxpay/dopay', $param, true, 'wx'));
        }else {
            $this->error('order is empty', url('/order/orderlist', '', true, 'user'));
        }
    }   


}

